#include <bits/stdc++.h>
using namespace std;
int n, m;
typedef pair<int, int> pi;
const int N = 110, M = N << 2;
int h[N], e[M], w[M], ne[M], idx;
int dist[N];
bool st[N];
void add(int a, int b, int c) {
  e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
int dijkstra() {
  int res = -1, cnt = 0;
  priority_queue<pi, vector<pi>, greater<> > pq;
  memset(dist, 0x3f, sizeof dist);
  dist[1] = 0;
  pq.push({0, 1});
  while (pq.size()) {
    auto [d, u] = pq.top();
    pq.pop();
    if (st[u]) continue;
    st[u] = true;
    cnt++;
    res = max(res, d);
    for (int i = h[u]; ~i; i = ne[i]) {
      int v = e[i];
      if (dist[v] > dist[u] + w[i]) {
        dist[v] = dist[u] + w[i];
        pq.push({dist[v], v});
      }
    }
  }
  return cnt == n ? res : -1;
}
int main() {
  memset(h, -1, sizeof h);
  cin >> n >> m;
  while (m--) {
    int a, b, c;
    cin >> a >> b >> c;
    add(a, b, c), add(b, a, c);
  }
  cout << dijkstra() << endl;
  return 0;
}
